package com.ccrfid.rmc6.pojo.dto.rollcall;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class RollcallResultNode extends Observable implements Observer{
    private Long orgId;
    private String orgName;
    private Integer total;
    private Integer attendance;
    private List<RollcallResultNode> children;
    private List<RollcallResultDto> rollcallDetail;

    public RollcallResultNode(Long orgId, String orgName) {
        this.orgId = orgId;
        this.orgName = orgName;
        this.total = 0;
        this.attendance = 0;
        this.children = new ArrayList<>();
        this.rollcallDetail = new ArrayList<>();
    }

    @Override
    public void update(Observable o, Object arg) {
        if (arg instanceof RollcallResultDto) {
            RollcallResultDto dto = (RollcallResultDto) arg;
            plusTotal();
            if (dto.getResult() != null && dto.getResult().equals(0)) {
                plusAttendance();
            }
            //向上传递
            setChanged();
            notifyObservers(dto);
        }
    }

    public void plusTotal() {
        this.total ++;
    }

    public void plusAttendance() {
        this.attendance ++;
    }

    public void addChild(RollcallResultNode node) {
        if (children == null) {
            children = new ArrayList<>();
        }
        children.add(node);
        //添加父节点为观察者
        node.addObserver(this::update);
    }

    public void addRollcallResult(RollcallResultDto dto) {
        if (rollcallDetail == null) {
            rollcallDetail = new ArrayList<>();
        }
        rollcallDetail.add(dto);
        plusTotal();
        if (dto.getResult() != null && dto.getResult().equals(0)) {
            plusAttendance();
        }
        //通知父节点
        setChanged();
        notifyObservers(dto);
    }

    public Long getOrgId() {
        return orgId;
    }

    public void setOrgId(Long orgId) {
        this.orgId = orgId;
    }

    public String getOrgName() {
        return orgName;
    }

    public void setOrgName(String orgName) {
        this.orgName = orgName;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getAttendance() {
        return attendance;
    }

    public void setAttendance(Integer attendance) {
        this.attendance = attendance;
    }

    public List<RollcallResultNode> getChildren() {
        return children;
    }

    public void setChildren(List<RollcallResultNode> children) {
        this.children = children;
    }

    public List<RollcallResultDto> getRollcallDetail() {
        return rollcallDetail;
    }

    public void setRollcallDetail(List<RollcallResultDto> rollcallDetail) {
        this.rollcallDetail = rollcallDetail;
    }
}
